package com.redstars.generate.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.redstars.generate.entity.SysBaseClass;
import com.redstars.generate.exception.RedstarsException;
import com.redstars.generate.page.PageResult;
import com.redstars.generate.utils.PageUtils;
import lombok.AllArgsConstructor;
import com.redstars.generate.convert.SysBaseClassConvert;
import com.redstars.generate.query.SysBaseClassQuery;
import com.redstars.generate.vo.SysBaseClassVo;
import com.redstars.generate.dao.SysBaseClassDao;
import com.redstars.generate.service.SysBaseClassService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.List;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;

/**
 * 基类管理
 *
 * @author zhouhx 
 * @since 1.0.0 2022-10-05
 */
@Service
@AllArgsConstructor
public class SysBaseClassServiceImpl extends ServiceImpl<SysBaseClassDao, SysBaseClass> implements SysBaseClassService {

    @Override
    public PageResult<SysBaseClassVo> page(SysBaseClassQuery query) {
        LambdaQueryWrapper<SysBaseClass> wrapper = getWrapper(query);
        PageUtils<SysBaseClass> pageUtils = new PageUtils<>();
        IPage<SysBaseClass> page = this.page(pageUtils.getPage(query), wrapper);

        return pageUtils.getPageData(page,SysBaseClassVo.class);
    }

    private LambdaQueryWrapper<SysBaseClass> getWrapper(SysBaseClassQuery query){
        LambdaQueryWrapper<SysBaseClass> wrapper = Wrappers.lambdaQuery();

        return wrapper;
    }

    @Override
    public void save(SysBaseClassVo vo) {
        SysBaseClass entity = SysBaseClassConvert.INSTANCE.convert(vo);

        baseMapper.insert(entity);
    }

    @Override
    public void update(SysBaseClassVo vo) {
        SysBaseClass entity = SysBaseClassConvert.INSTANCE.convert(vo);

        updateById(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(List<Long> idList) {
        removeByIds(idList);
    }

    @Override
    public SysBaseClassVo detail(Long id) {
        SysBaseClass detail = this.getById(id);
        if (ObjectUtil.isEmpty(detail)) {
        throw new RedstarsException("记录不存在");
        }
        return BeanUtil.copyProperties(detail,SysBaseClassVo.class);
    }

    @Override
    public List<SysBaseClassVo> list(SysBaseClassQuery query) {
        LambdaQueryWrapper<SysBaseClass> wrapper = getWrapper(query);
        List<SysBaseClass> list = baseMapper.selectList(wrapper);
        return SysBaseClassConvert.INSTANCE.convertList(list);
    }

}